<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试页面</title>
</head>
<body>
测试页面
<div id="err">
</div>
<img id="userImg" src="" alt="">
</body>
<script type="text/javascript" src="http://g.alicdn.com/dingding/open-develop/1.6.9/dingtalk.js"></script>
<script type="text/javascript" src="./static/jslib/jquery1.12.4.js"></script>
<script type="text/javascript">
var rest_get_token = '<%=request.getContextPath()%>/openapi/v1/dingtalk/mapp/ddConfig';
rest_get_token += '?pageUrl=' + encodeURIComponent(window.location.href);
var rest_get_curuser = '<%=request.getContextPath()%>/openapi/v1/dingtalk/mapp/currentUser';
rest_get_curuser += '?code=';

function onDdConfigLoaded(_config) {
    console.log(_config);
    dd.config({
        agentId : _config.agentid,
        corpId : _config.corpId,
        timeStamp : _config.timeStamp,
        nonceStr : _config.nonceStr,
        signature : _config.signature,
        jsApiList : [ 'runtime.info', 'biz.contact.choose',
            'device.notification.confirm', 'device.notification.alert',
            'device.notification.prompt', 'biz.ding.post',
            'biz.util.openLink' ]
    });

    dd.ready(function() {
        dd.biz.navigation.setTitle({
            title: _config.appName,
            onSuccess: function(data) {
                console.log(JSON.stringify(data));
            },
            onFail: function(err) {
                console.log('err='+ JSON.stringify(err));
            }
        });

        dd.runtime.info({
            onSuccess : function(info) {
                console.log('runtime info: ' + JSON.stringify(info));
                $('#err').append('runtime info: ' + JSON.stringify(info));
            },
            onFail : function(err) {
                console.log('fail: ' + JSON.stringify(err));
                //$('#err').append(JSON.stringify(err));
            }
        });

        dd.runtime.permission.requestAuthCode({
            corpId : _config.corpId,
            onSuccess : function(info) {
                alert(rest_get_curuser + info.code + '&ddAppId='+_config.appId);
                $.ajax({
                    url : rest_get_curuser + info.code + '&ddAppId='+_config.appId,
                    async: true,
                    cache: false,
                    type : 'GET',
                    success : function(data, status, xhr) {
                        alert(JSON.stringify(data));
                        $('#err').append(JSON.stringify(data));

                        // 图片
                        if(data.avatar.length != 0){
                            var img = document.getElementById("userImg");
                            img.src = data.avatar;
                            img.height = '100';
                            img.width = '100';
                        }

                    },
                    error : function(xhr, errorType, error) {
                        console.log("error corpId:" + _config.corpId);
                        alert(errorType + ', ' + error);
                    }
                });

            },
            onFail : function(err) {
                alert('fail: ' + JSON.stringify(err));
            }
        });
    });
}

$(document).ready(function () {
    $.ajax({
        url : rest_get_token,
        method: 'get',
        cache: false,
        async: true,
        success: onDdConfigLoaded,
        error: function (err) {
            alert('fail: ' + JSON.stringify(err));
        }
    });
});

</script>
</html>
